package com.swd2010.mobileSearch;

import com.swd2010.mobileSearch.data.internalTransfer.ApplicationDataTransfer;
import com.swd2010.mobileSearch.network.ConnectInformation;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class EinstellungenActivity extends Activity implements OnCheckedChangeListener, OnClickListener {
	
	
	private ToggleButton shakeDetectionButton;
	
	private EditText     serviceUrlTextView;
	
	private Button       saveNetworkUrlButton;
	
	
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.einstellungen_layout);
        
        
        shakeDetectionButton = (ToggleButton)  findViewById(R.id.toggleButton1);
        
        serviceUrlTextView   = (EditText)  findViewById(R.id.NetworkAdressTextViewId);
        
        saveNetworkUrlButton   = (Button)  findViewById(R.id.saveNetworkAddressButtonId);
        
        
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
                
        String shakeDetection = sharedPref.getString(getString(R.string.ShakeDetection), "Y");
        
        String serviceBaseUrl = sharedPref.getString(getString(R.string.Service_Domain_Url), "http://10.0.2.2:8080/");
        
        
        
        if ( shakeDetection.equals("Y") ){
        	shakeDetectionButton.setChecked(true);
        }
        else{
        	shakeDetectionButton.setChecked(false);
        }
        
        
        shakeDetectionButton.setOnCheckedChangeListener(this);
        
        saveNetworkUrlButton.setOnClickListener(this);
        
        serviceUrlTextView.setText(serviceBaseUrl);
        
        
    }


	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


		String strToSave = new String("Y");
		
		if ( isChecked == false ){
			strToSave = "N";
		}
		
		SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
		
		SharedPreferences.Editor editor = sharedPref.edit();
		
		editor.putString(getString(R.string.ShakeDetection), strToSave);
		
		editor.commit();				
		
		
	}


	public void onClick(View v) {

		
		
		SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
		
		SharedPreferences.Editor editor = sharedPref.edit();
		
		editor.putString(getString(R.string.Service_Domain_Url), serviceUrlTextView.getText().toString());
		
		editor.commit();						
		
		
		ApplicationDataTransfer.getInstance().setBaseUrl(serviceUrlTextView.getText().toString());
		
	}
}